package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ProdDetailItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class ProdDetailThread extends BasicThread {

	public interface ProdDetailThreadCallback {
		public void OnProdDetailCompleted(boolean isNetworkConntected
				, ProdDetailItem prodItem, String message);
		public void OnProdDetailError(ErrorItem errorItem);
	}

	private String url;
	private ProdDetailThreadCallback callback;
	private HashMap<String, String> searchMap;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	
	public ProdDetailThread(Context context,
			ProdDetailThreadCallback callback, HashMap<String, String> searchMap
			, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.searchMap = searchMap;
	}

	@Override
	public void run() {
		Map<String,String> map = new HashMap<String, String>();
		map.put(Const.RESOFFCD, searchMap.get("resOffCd").toString());
		map.put(Const.RESNO, searchMap.get("resNo").toString());
		map.put(Const.RESSEQ, searchMap.get("resSeq").toString());
		String param = "";
		param = parameterReturn(Const.REQ020, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnProdDetailCompleted(false, null, "통신 에러");
			return;
		}
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnProdDetailError(null);
				return;
			}
			Log.i("json","json = " + json);
			int result3 = json.getInt(ProdDetailItem.RESULT3);
			int result4 = json.getInt(ProdDetailItem.RESULT4);
			int result5 = json.getInt(ProdDetailItem.RESULT5);
			int result6 = json.getInt(ProdDetailItem.RESULT6);
			
			JSONArray dataArray1 = json.getJSONArray(ProdDetailItem.DATA1);
			ProdDetailItem prodDetailItem = null;
			JSONObject prod1 = dataArray1.getJSONObject(0);
			
			prodDetailItem = new ProdDetailItem();
			// data1
			prodDetailItem.setAllInsttNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod1, ProdDetailItem.ALLINSTTNM)));
			prodDetailItem.setFullRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod1, ProdDetailItem.FULLRECNO)));
			prodDetailItem.setReqstDate(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod1, ProdDetailItem.REQSTDATE)));
			prodDetailItem.setNatNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod1, ProdDetailItem.NATNM)));
			prodDetailItem.setTraDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod1, ProdDetailItem.TRADE)));
			prodDetailItem.setVenNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod1, ProdDetailItem.VENNM)));
			prodDetailItem.setTrnNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod1, ProdDetailItem.TRNNM)));
			prodDetailItem.setMetNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod1, ProdDetailItem.METNM)));
			prodDetailItem.setFileList(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod1, ProdDetailItem.FILELIST)));

			// data2
			JSONArray dataArray2 = json.getJSONArray(ProdDetailItem.DATA2);
			JSONObject prod2 = dataArray2.getJSONObject(0);
			prodDetailItem.setFullResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod2, ProdDetailItem.FULLRESNO)));
			prodDetailItem.setValidflagNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod2, ProdDetailItem.VALIDFLAGNM)));
			prodDetailItem.setFieDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod2, ProdDetailItem.FIEDE)));
			prodDetailItem.setDspsTrgetNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod2, ProdDetailItem.DSPSTRGETNM)));
			prodDetailItem.setFieArea(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod2, ProdDetailItem.FIEAREA)));
			prodDetailItem.setQuranNm1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod2, ProdDetailItem.QURANNM1)));
			prodDetailItem.setQuranNm2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod2, ProdDetailItem.QURANNM2)));
			prodDetailItem.setResultProgrsNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod2, ProdDetailItem.RESULTPROGRSNM)));
			prodDetailItem.setDrafLastDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod2, ProdDetailItem.DRAFLASTDE)));
			prodDetailItem.setAftDsuseTrgetNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod2, ProdDetailItem.AFTDSUSETRGETNM)));
			prodDetailItem.setRemark(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod2, ProdDetailItem.REMARK)));
			
			if(result3 > 0){
				JSONArray dataArray3 = json.getJSONArray(ProdDetailItem.DATA3);
				JSONObject prod3 = dataArray3.getJSONObject(0);
				prodDetailItem.setData3("Y");
				prodDetailItem.setLabDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod3, ProdDetailItem.LABDE)));
				prodDetailItem.setLabQuranNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod3, ProdDetailItem.LABQURANNM)));
				prodDetailItem.setLabOpin(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod3, ProdDetailItem.LABOPIN)));
				prodDetailItem.setLabEqu(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod3, ProdDetailItem.LABEQU)));
				prodDetailItem.setPassYn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod3, ProdDetailItem.PASSYN)));
				prodDetailItem.setStateTot(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod3, ProdDetailItem.STATETOT)));
			}else{
				prodDetailItem.setData3("N");
			}
			
			if(result4 > 0){
				JSONArray dataArray4 = json.getJSONArray(ProdDetailItem.DATA4);
				JSONObject prod4 = dataArray4.getJSONObject(0);
				prodDetailItem.setData4("Y");
				prodDetailItem.setDbyhsNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod4, ProdDetailItem.DBYHSNM)));
				prodDetailItem.setProNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod4, ProdDetailItem.PRONM)));
				prodDetailItem.setDamDegNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod4, ProdDetailItem.DAMDEGNM)));
				prodDetailItem.setRegNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod4, ProdDetailItem.REGNM)));
				prodDetailItem.setSamNoVeiw(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod4, ProdDetailItem.SAMNOVEIW)));
				prodDetailItem.setOrgPla(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod4, ProdDetailItem.ORGPLA)));
			}else{
				prodDetailItem.setData4("N");
			}
			
			if(result5 > 0){
				JSONArray dataArray5 = json.getJSONArray(ProdDetailItem.DATA5);
				JSONObject prod5 = dataArray5.getJSONObject(0);
				prodDetailItem.setData5("Y");
				prodDetailItem.setFullDsnfResN0(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod5, ProdDetailItem.FULLDSNFRESN0)));
				prodDetailItem.setPasDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod5, ProdDetailItem.PASDE)));
				prodDetailItem.setAntDatefr(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod5, ProdDetailItem.ANTDATEFR)));
				prodDetailItem.setAntDateto(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod5, ProdDetailItem.ANTDATETO)));
				prodDetailItem.setFullDnsfCmmdNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod5, ProdDetailItem.FULLDSNFRESN0)));
				prodDetailItem.setDnsfCmmDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod5, ProdDetailItem.DNSFCMMDE)));
				prodDetailItem.setSanghos(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod5, ProdDetailItem.SANGHOS)));
				prodDetailItem.setAntArea(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod5, ProdDetailItem.ANTAREA)));
			}else{
				prodDetailItem.setData5("N");
			}
			
			if(result6 > 0){
				JSONArray dataArray6 = json.getJSONArray(ProdDetailItem.DATA6);
				JSONObject prod6 = dataArray6.getJSONObject(0);
				prodDetailItem.setData6("Y");
				prodDetailItem.setFullDsuseResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod6, ProdDetailItem.FULLDSUSERESNO)));
				prodDetailItem.setWasDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod6, ProdDetailItem.WASDE)));
				prodDetailItem.setActDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod6, ProdDetailItem.ACTDE)));
				prodDetailItem.setWorArea(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod6, ProdDetailItem.WORAREA)));
				prodDetailItem.setFullDsuseCmmdNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod6, ProdDetailItem.FULLDSUSECMMDNO)));
				prodDetailItem.setDsuseCmmdDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod6, ProdDetailItem.DSUSECMMDDE)));
				prodDetailItem.setSelDsuseNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod6, ProdDetailItem.SELDSUSENM)));
			}else{
				prodDetailItem.setData6("N");
			}
			
			JSONArray dataArray7 = json.getJSONArray(ProdDetailItem.DATA7);
			JSONObject prod7 = dataArray7.getJSONObject(0);
			
			prodDetailItem.setRecSeq(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod7, ProdDetailItem.RECSEQ)));
			prodDetailItem.setPrdlstCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod7, ProdDetailItem.PRDLSTCODE)));
			prodDetailItem.setPrdlstNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod7, ProdDetailItem.PRDLSTNM)));
			prodDetailItem.setInsQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod7, ProdDetailItem.INSQTY)));
			prodDetailItem.setItmQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod7, ProdDetailItem.ITMQTY)));
			prodDetailItem.setQunKor(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod7, ProdDetailItem.QUNKOR)));
			prodDetailItem.setItemType(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod7, ProdDetailItem.ITEMTYPE)));
			callback.OnProdDetailCompleted(true, prodDetailItem, mResult);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/*JSONParser jParser = new JSONParser();

		url = Const.MAIN_URL;
		post = ParameterUtil.getPostProdDetail(searchMap);
		Log.i("post","post = " + post);
		
		JSONObject json = jParser.getJSONFromUrl(url, post);*/
		

		/*try {
			
			
		} catch (JSONException e) {
			
		}*/
	}
}